package com.example.jackpeng.circlemenu;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by jack.peng on 2016/5/10.Mko9AQ
 */
public class CircleMenuLayout extends ViewGroup {
    // 圆形直径
    private int mRadius;
    // 该容器内child item的默认尺寸
    private static final float RADIO_DEFAULT_CHILD_DIMENSION = 1 / 4f;
    // 该容器的内边距,无视padding属性，如需边距请用该变量
    private static final float RADIO_PADDING_LAYOUT = 1 / 12f;
    // 该容器的内边距,无视padding属性，如需边距请用该变量
    private float mPadding;
    // 布局时的开始角度
    private double mStartAngle = 0;
    // 菜单项的文本
    private String[] mItemTexts;
    // 菜单项的图标
    private int[] mItemImgs;
    // 菜单的个数
    private int mMenuItemCount;
    // 菜单布局资源id
    private int mMenuItemLayoutId = R.layout.circle_menu_item;
    // MenuItem的点击事件接口
    private AdapterView.OnItemClickListener mOnMenuItemClickListener;
    private Context mContext;

    public CircleMenuLayout(Context context,AttributeSet attrs){
        super(context,attrs);
        //无视padding
        mContext = context;
        setPadding(0, 0, 0, 0);
    }

    // 设置菜单条目的图标和文本
    public void setMenuItemIconsAndTexts(int[] images,String[] texts){
        if(images == null && texts == null){
            throw new IllegalArgumentException("菜单项文本和图片至少设置其一");
        }

        mItemImgs = images;
        mItemTexts = texts;
        // 初始化mMenuCount
        mMenuItemCount = images==null? texts.length:images.length;
        if(images!=null && texts!=null){
            mMenuItemCount = Math.min(images.length,texts.length);
        }
        // 构建菜单项
        buildMenuItems();
    }

    private void buildMenuItems(){
        for(int i = 0;i<mMenuItemCount;i++){
            View itemView = inflateMenuView(i);
            initMenuItem(itemView,i);
            addView(itemView);
        }
    }

    private View inflateMenuView(final int chileIndex){
        LayoutInflater mInflater = LayoutInflater.from(getContext());
        View itemView = mInflater.inflate(mMenuItemLayoutId, this, false);
        itemView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnMenuItemClickListener != null) {
                    mOnMenuItemClickListener.onItemClick(v, chileIndex);
                }
            }
        });
        return itemView;
    }

    private View initMenuItem(View itemView ,int childIndex){
        ImageView iv = (ImageView)itemView.findViewById(R.id.icon);
        TextView tv = (TextView) findViewById(R.id.text);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(mItemImgs[childIndex]);
        tv.setVisibility(View.VISIBLE);
        tv.setText(mItemTexts[childIndex]);
        return itemView;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
        this.mOnMenuItemClickListener = listener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int resWidth = 0;
        int resHeight = 0;
        // 根据传入的参数，分别获取测量模式和测量值
        int widht = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        // 如果宽或者高的测量模式非精确值
        if(widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY){
            resWidth = getSuggestedMinimumWidth();
            resWidth = resWidth == 0 ? getDefaultWidth():resWidth;
            resHeight = getSuggestedMinimumHeight();
            resHeight = resHeight == 0 ? getDefaultHeigt():resHeight;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private int getDefaultWidth(){
        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();//.getWidth();
        Point size = new Point();
        display.getSize(size);
        return size.x;
    }
}
